<?php
namespace admin\module\questionBank;
use inc\BaseAjax\BaseAjax;
require('../../../global.php');
class ajax extends BaseAjax
{
    protected function listQuestionCategory()
    {
        global $db;
        if(isset($_GET['search']) && !empty($_GET['search'])){
            $where['AND']['name[~]'] = $_GET['search'];
        }
        if(isset($_GET['sort']) && !empty($_GET['sort']) && isset($_GET['order']) && !empty($_GET['order'])){
            $where['ORDER'] = $_GET['sort'].' '.strtoupper($_GET['order']);
        }
        $echo['total'] = $db->count('tb_question_category', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_question_category', '*', $where);
        foreach ($row as $key => $value) {
            $tmp['name'] = $value['name'];
            $tmp['createtime'] = $value['createtime'];
            if(checkAdminAuthorize('questionBank.edit')){
                $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">编辑</a> ';
                
            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    /*
     * @author : bapijun
     * 根据参数展示对应的题库
     */
    protected function listQuestionBank()
    {
        global $db;
        if(isset($_GET['search']) && !empty($_GET['search'])){
            $where['AND']['question[~]'] = $_GET['search'];
        }
        
        if(isset($_GET['sort']) && !empty($_GET['sort']) && isset($_GET['order']) && !empty($_GET['order'])){
            $where['ORDER'] = $_GET['sort'].' '.strtoupper($_GET['order']);
        }
        $echo['total'] = $db->count('tb_question_bank', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_question_bank', '*', $where);
        foreach ($row as $key => $value) {
            
            $tmp['question'] = $value['question'];
            $tmp['category_id'] = $db->get('tb_question_category', 'name', array('tbid' => $value['category_id']));
            if(checkAdminAuthorize('questionBank.edit')){
                $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">编辑</a> ';
                $tmp['do'] .= '<button type="button" class="btn btn-danger btn-outline btn-xs remove" data-id="'.$value['tbid'].'">删除</button> ';
            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    /*
     * @author : bapijun
     * 添加对应的题库类目
     */
    protected function add()
    {
        global $db;
        $set = array(
            'question' => $_POST['question'],
            'category_id' => $_POST['cid'],
            'edittime' => date('Y-m-d H:i:s'),
        );
        if (!checkVar($_POST['id'])) {
            $set['createtime'] = date('Y-m-d H:i:s');
            $rs = $db->insert('tb_question_bank', $set);
            if($rs){
                $cb['status'] = 'y';
            }else{
                $cb['status'] = 'n';
            }
        } else {
            $rs = $db->update('tb_question_bank', $set, array('tbid' => $_POST['id']));
            $cb['status'] = 'y';
        }
        
        echo json_encode($cb);
    }
    

    /*
     * @author : bapijun
     * 新增或编辑对应的测试题库类目
     */
    protected function addCategory()
    {
        global $db;
        $set = array(
            'name' => $_POST['name'],
            'edittime' => date('Y-m-d H:i:s'), 
        );
        //新增类目
        if (!checkVar($_POST['id'])) {
            $set['createtime'] = date('Y-m-d H:i:s');
            $rs = $db->insert('tb_question_category', $set);
            if ($rs) {
                 $cb['status'] = 'y';
            } else {
                $cb['status'] = 'n';
            }
        } else {//编辑类目
            $rs = $db->update('tb_question_category', $set, array('tbid' => $_POST['id']));
            $cb['status'] = 'y';
        }
        echo json_encode($cb);
    }
    
    protected function delete()
    {
        global $db;
        $rs = $db->delete('tb_question_bank', array('tbid' => $_POST['id']));
        if ($rs) {
           $cb['status'] = 'y';
        } else {
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }
    
    /*
     * @author : bapijun
     * 获取对应的类目,返回给选择类目页面
     */
    protected function getCategory()
    {
        global $db;
        $category = $db->select('tb_question_category', '*');
        $cb = $this->getAllCategory(0, $category);
        echo json_encode_ex($cb);
        
    }
    /*
     * @author : bapijun
     * 递归调用,将数组形式的类目,转化为可被调用的树的形式
     */
    private function getAllCategory($parent_category_id, $category)
    {
        
        foreach($category as $v){
            if($v['parent_category_id'] == $parent_category_id){
                $tmp['id'] = $v['tbid'];
                $tmp['text'] = $v['name'];
                $tmp['nodes'] = $this->getAllCategory($v['id'], $category);
                $cb[] = $tmp;
            }
        }
        return $cb;
    }
    
}

$ajax = new ajax();
$ajax->dealWithAjax();

